package com.pca.headline.controller;

import com.pca.headline.common.Result;
import com.pca.headline.pojo.NewsHeadline;
import com.pca.headline.pojo.NewsType;
import com.pca.headline.pojo.vo.HeadlineDetailVo;
import com.pca.headline.pojo.vo.HeadlineQueryVo;
import com.pca.headline.service.NewsHeadlineService;
import com.pca.headline.service.NewsTypeService;
import com.pca.headline.service.impl.NewsHeadlineServiceImpl;
import com.pca.headline.service.impl.NewsTypeServiceImpl;
import com.pca.headline.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/portal/*")
public class PortalController extends BaseController {
    private NewsTypeService newsTypeService = new NewsTypeServiceImpl();
    private NewsHeadlineService headlineService = new NewsHeadlineServiceImpl();

    //查询所有类型
    protected void findAllTypes(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<NewsType> newsTypeList = newsTypeService.findAllTypes();

        WebUtil.writeJson(resp, Result.ok(newsTypeList));

    }


    protected void findNewsPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取数据
        HeadlineQueryVo headlineQuery = WebUtil.readJson(req, HeadlineQueryVo.class);

        Map pageInfo = headlineService.findPage(headlineQuery);
        Map data = new HashMap();
        data.put("pageInfo", pageInfo);

        Result result = Result.ok(data);
        WebUtil.writeJson(resp,result);

    }



    protected void showHeadlineDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String hid = req.getParameter("hid");
        int id = hid == null ? -1 : Integer.parseInt(hid);
        HeadlineDetailVo headlineDetail = (HeadlineDetailVo) headlineService.findHeadlineDetail(id);
        Map data = new HashMap();
        data.put("headline", headlineDetail);
        Result result = Result.ok(data);
        WebUtil.writeJson(resp,result);

    }
}
